package com.jic.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.constant.consist.ResponseCode;
import com.jic.product.entity.Product;
import com.jic.product.entity.ProductUnits;
import com.jic.product.entity.StoreInventory;
import com.jic.product.exception.AppRuntimeException;
import com.jic.product.mapper.*;
import com.jic.product.model.entity.MStore;
import com.jic.product.model.entity.MStoreDicts;
import com.jic.product.model.entity.MStoreOperateLog;
import com.jic.product.model.entity.MStoreOperateProductlog;
import com.jic.product.model.vo.request.MStoreOperateRequest;
import com.jic.product.model.vo.response.MStoreCheckResponse;
import com.jic.product.model.vo.response.MStoreOperateProductInfoResponse;
import com.jic.product.model.vo.response.MStoreOperateResponse;
import com.jic.product.service.MStoreOperateService;
import com.jic.product.service.StoreInventoryService;
import com.jic.product.util.ExcelImportUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 接口实现类
 *
 * @author : 建投数据科技(山东)有限公司
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
@Transactional
public class MStoreOperateServiceImpl implements MStoreOperateService {

    @Resource
    private MStoreOperateLogMapper mapper;

    @Resource
    private StoreInventoryMapper storeInventoryMapper;

    @Resource
    private MStoreDictsMapper dictsMapper;

    @Resource
    private MStoreOperateProductLogMapper mStoreOperateProductLogMapper;

    @Resource
    private MStoreMapper mStoreMapper;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductUnitsMapper productUnitsMapper;

    @Resource
    private StoreInventoryService storeInventoryService;



    /**
     * 新增记录
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存成功
     */
    @Override
    public int save(MStoreOperateLog request) {
        int id = mapper.insert(request);
        if (id > 0) {
            return id;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
        }
    }


    /**
     * 保存或修改记录信息
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存或修改成功
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:09
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveOrUpdate(MStoreOperateRequest request) {
        log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
        try {
            if (request == null) {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "入参为空");
            }
            if (request.getType() == 1) {
                saveAffair(request);
            } else {
                saveInventory(request);
            }
        } catch (Exception e) {
            log.error("用户保存或修改失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "用户保存或修改失败");
        }
        return Boolean.TRUE;
    }

    //库内事务单据
    public void saveAffair(MStoreOperateRequest request) {
        if (request.getId() == null) {
            //保存基本信息
            MStoreOperateLog mStoreOperate = new MStoreOperateLog();
            BeanUtils.copyProperties(request, mStoreOperate);
            mStoreOperate.setDeleteFlag(0);
            mStoreOperate.setCreator(request.getAdminOperate().getOperator());
            mStoreOperate.setCreateTime(new Date());
            MStoreDicts mStoreDicts = new MStoreDicts();
            mStoreDicts.setId(request.getOperateId());
            MStoreDicts dicts = dictsMapper.selectByPrimaryKey(mStoreDicts);
            if (null != dicts) {
                mStoreOperate.setOperateDesc(dicts.getOptionLabel());
            }
            mStoreDicts.setDeptId(request.getAdminOperate().getDeptId());
            mapper.insertSelective(mStoreOperate);
            //商品信息
            if (CollectionUtils.isNotEmpty(request.getProducts())) {
                request.getProducts().forEach(item -> {
                    MStoreOperateProductlog mStoreOperateProductlog = new MStoreOperateProductlog();
                    BeanUtils.copyProperties(item, mStoreOperateProductlog);
                    mStoreOperateProductlog.setOperateId(mStoreOperate.getId());
                    mStoreOperateProductlog.setProductName(item.getChineseName());
                    mStoreOperateProductlog.setProductBarCode(item.getBarCode());
                    mStoreOperateProductlog.setArtNo(item.getArtNo());
                    mStoreOperateProductlog.setProductCode(item.getCode());
                    mStoreOperateProductlog.setStoreId(request.getStoreId());
                    mStoreOperateProductlog.setDeptId(request.getDeptId());
                    mStoreOperateProductlog.setCreateTime(new Date());
                    mStoreOperateProductlog.setCreator(request.getAdminOperate().getOperator());
                    mStoreOperateProductLogMapper.insertSelective(mStoreOperateProductlog);
                    if (request.getStatus() == 1) {
                        //修改库存
                        StoreInventory storeInventory = new StoreInventory();
                        storeInventory.setDeleteFlag(0);
                        storeInventory.setProductId(item.getProductId());
                        List<StoreInventory> inventoryList = storeInventoryMapper.query(storeInventory);
                        if (CollectionUtils.isEmpty(inventoryList)) {
                            //不存在库存记录--入库
                            storeInventory.setCreator(request.getAdminOperate().getOperator());
                            storeInventory.setAvailableStock(Integer.valueOf(item.getActualNum().toString()));
                            storeInventory.setCreateTime(new Date());
                            storeInventoryMapper.insert(storeInventory);
                        } else {
                            //判断 入库/出库
                            MStoreDicts mStoreDicts1 = new MStoreDicts();
                            mStoreDicts.setId(request.getOperateId());
                            MStoreDicts dicts1 = dictsMapper.selectByPrimaryKey(mStoreDicts1);
                            if (null != dicts1) {
                                if (dicts1.getType().equals("1")) {
                                    storeInventory.setId(inventoryList.get(0).getId());
                                    storeInventory.setAvailableStock(inventoryList.get(0).getAvailableStock() +
                                            Integer.valueOf(item.getActualNum().toString()));
                                    storeInventory.setUpdater(request.getAdminOperate().getOperator());
                                    storeInventory.setUpdateTime(new Date());
                                    storeInventoryMapper.update(storeInventory);
                                } else {
                                    storeInventory.setUpdater(request.getAdminOperate().getOperator());
                                    storeInventory.setId(inventoryList.get(0).getId());
                                    storeInventory.setAvailableStock(inventoryList.get(0).getAvailableStock() -
                                            Integer.valueOf(item.getActualNum().toString()));
                                    storeInventory.setUpdateTime(new Date());
                                    storeInventoryMapper.update(storeInventory);
                                }
                            }
                        }
                    }
                });
            }
        } else {
            //修改基本信息
            MStoreOperateLog mStoreOperate = new MStoreOperateLog();
            BeanUtils.copyProperties(request, mStoreOperate);
            mStoreOperate.setUpdateTime(new Date());
            mStoreOperate.setUpdater(request.getAdminOperate().getOperator());
            mapper.updateByPrimaryKeySelective(mStoreOperate);
            //商品信息
            if (CollectionUtils.isNotEmpty(request.getProducts())) {
                //先删除
                mStoreOperateProductLogMapper.deleteByOperateId(request.getId());
                request.getProducts().forEach(item -> {
                    MStoreOperateProductlog mStoreOperateProductlog = new MStoreOperateProductlog();
                    BeanUtils.copyProperties(item, mStoreOperateProductlog);
                    mStoreOperateProductlog.setStoreId(request.getStoreId());
                    mStoreOperateProductlog.setDeptId(request.getDeptId());
                    mStoreOperateProductlog.setCreateTime(new Date());
                    mStoreOperateProductlog.setCreator(request.getAdminOperate().getOperator());
                    mStoreOperateProductLogMapper.insertSelective(mStoreOperateProductlog);
                    if (request.getStatus() == 1) {
                        //修改库存
                        StoreInventory storeInventory = new StoreInventory();
                        storeInventory.setDeleteFlag(0);
                        storeInventory.setProductId(item.getProductId());
                        List<StoreInventory> inventoryList = storeInventoryMapper.query(storeInventory);
                        if (CollectionUtils.isEmpty(inventoryList)) {
                            //不存在库存记录--入库
                            storeInventory.setCreator(request.getAdminOperate().getOperator());
                            storeInventory.setAvailableStock(Integer.valueOf(item.getActualNum().toString()));
                            storeInventory.setCreateTime(new Date());
                            storeInventoryMapper.insert(storeInventory);
                        } else {
                            //判断 入库/出库
                            MStoreDicts mStoreDicts = new MStoreDicts();
                            mStoreDicts.setId(request.getOperateId());
                            MStoreDicts dicts = dictsMapper.selectByPrimaryKey(mStoreDicts);
                            if (null != dicts) {
                                if (dicts.getType().equals("1")) {
                                    storeInventory.setId(inventoryList.get(0).getId());
                                    storeInventory.setAvailableStock(inventoryList.get(0).getAvailableStock() +
                                            Integer.valueOf(item.getActualNum().toString()));
                                    storeInventory.setUpdateTime(new Date());
                                    storeInventory.setUpdater(request.getAdminOperate().getOperator());
                                    storeInventoryMapper.update(storeInventory);
                                } else {
                                    storeInventory.setId(inventoryList.get(0).getId());
                                    storeInventory.setAvailableStock(inventoryList.get(0).getAvailableStock() -
                                            Integer.valueOf(item.getActualNum().toString()));
                                    storeInventory.setUpdateTime(new Date());
                                    storeInventory.setUpdater(request.getAdminOperate().getOperator());
                                    storeInventoryMapper.update(storeInventory);
                                }
                            }
                        }
                    }
                });
            }
        }
    }

    //盘点
    public void saveInventory(MStoreOperateRequest request) {
        if (request.getId() == null) {
            //保存基本信息
            MStoreOperateLog mStoreOperate = new MStoreOperateLog();
            BeanUtils.copyProperties(request, mStoreOperate);
            mStoreOperate.setCreator(request.getAdminOperate().getOperator());
            mStoreOperate.setCreateTime(new Date());
            mStoreOperate.setDeleteFlag(0);
            mStoreOperate.setDeptName(request.getDeptName());
            mStoreOperate.setDeptId(request.getAdminOperate().getDeptId());
            mapper.insertSelective(mStoreOperate);
            //商品信息
            if (CollectionUtils.isNotEmpty(request.getProducts())) {
                request.getProducts().forEach(item -> {
                    MStoreOperateProductlog mStoreOperateProductlog = new MStoreOperateProductlog();
                    BeanUtils.copyProperties(item, mStoreOperateProductlog);
                    mStoreOperateProductlog.setProductName(item.getChineseName());
                    mStoreOperateProductlog.setCreator(request.getAdminOperate().getOperator());
                    mStoreOperateProductlog.setCreateTime(new Date());
                    mStoreOperateProductlog.setProductBarCode(item.getBarCode());
                    mStoreOperateProductlog.setArtNo(item.getArtNo());
                    mStoreOperateProductlog.setProductCode(item.getCode());
                    mStoreOperateProductlog.setStoreId(request.getStoreId());
                    mStoreOperateProductlog.setDeptId(request.getDeptId());
                    mStoreOperateProductlog.setCreateTime(new Date());
                    mStoreOperateProductlog.setOperateId(mStoreOperate.getId());
                    mStoreOperateProductLogMapper.insertSelective(mStoreOperateProductlog);
                    if (request.getCheckStatus() == 3) {
                        //修改库存
                        StoreInventory storeInventory = new StoreInventory();
                        storeInventory.setDeleteFlag(0);
                        storeInventory.setProductId(item.getProductId());
                        List<StoreInventory> inventoryList = storeInventoryMapper.query(storeInventory);
                        if (CollectionUtils.isNotEmpty(inventoryList)) {
                            storeInventory.setId(inventoryList.get(0).getId());
                            storeInventory.setUpdater(request.getAdminOperate().getOperator());
                            storeInventory.setAvailableStock(Integer.valueOf(item.getCheckNum().toString()));
                            storeInventory.setUpdateTime(new Date());
                            storeInventoryMapper.update(storeInventory);
                        }
                    }
                });
            }
        } else {
            //修改基本信息
            MStoreOperateLog mStoreOperate = new MStoreOperateLog();
            BeanUtils.copyProperties(request, mStoreOperate);
            mStoreOperate.setUpdateTime(new Date());
            mStoreOperate.setUpdater(request.getAdminOperate().getOperator());
            mapper.updateByPrimaryKeySelective(mStoreOperate);
            //商品信息
            if (CollectionUtils.isNotEmpty(request.getProducts())) {
                //先删除
                mStoreOperateProductLogMapper.deleteByOperateId(request.getId());
                request.getProducts().forEach(item -> {
                    MStoreOperateProductlog mStoreOperateProductlog = new MStoreOperateProductlog();
                    BeanUtils.copyProperties(item, mStoreOperateProductlog);
                    mStoreOperateProductlog.setStoreId(request.getStoreId());
                    mStoreOperateProductlog.setDeptId(request.getDeptId());
                    mStoreOperateProductlog.setCreateTime(new Date());
                    mStoreOperateProductlog.setCreator(request.getAdminOperate().getOperator());
                    mStoreOperateProductLogMapper.insertSelective(mStoreOperateProductlog);
                    if (request.getCheckStatus() == 3) {
                        //修改库存
                        StoreInventory storeInventory = new StoreInventory();
                        storeInventory.setDeleteFlag(0);
                        storeInventory.setProductId(item.getProductId());
                        List<StoreInventory> inventoryList = storeInventoryMapper.query(storeInventory);
                        if (CollectionUtils.isNotEmpty(inventoryList)) {
                            storeInventory.setId(inventoryList.get(0).getId());
                            storeInventory.setAvailableStock(Integer.valueOf(item.getCheckNum().toString()));
                            storeInventory.setUpdateTime(new Date());
                            storeInventory.setUpdater(request.getAdminOperate().getOperator());
                            storeInventoryMapper.update(storeInventory);
                        }
                    }
                });
            }
        }

    }

    /**
     * 删除记录
     *
     * @param request 需要删除,含有主键的入参实体类
     * @return int 是否删除成功
     */
    @Override
    public int deleteByPrimaryKey(MStoreOperateLog request) {
        int flag = mapper.deleteByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除失败");
        }
    }


    /**
     * 逻辑删除记录
     *
     * @param request 含有主键的逻辑删除实体类
     * @return int 是否逻辑删除成功
     */
    @Override
    public int deleteByPrimaryKeyLogically(MStoreOperateLog request) {
        request.setDeleteFlag(1);
        int flag = mapper.updateByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
        }
    }


    /**
     * 修改
     *
     * @param request
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(MStoreOperateLog request) {
        try {
            log.info("修改记录");
            MStoreOperateLog param = new MStoreOperateLog();
            param.setUpdater(request.getAdminOperate().getOperator());
            param.setUpdateTime(new Date());
            BeanUtils.copyProperties(request, param);
            int flag = mapper.updateByPrimaryKeySelective(param);
            if (flag == 1) {
                return 1;
            } else {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
            }
        } catch (Exception e) {
            log.error("修改记录失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
        }

    }


    /**
     * 修改[库存操作流水记录表]的状态;
     *
     * @param request 新增或修改[库存操作流水记录表;]请求参数
     * @return MStoreOperateResponse 修改[库存操作流水记录表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @Override
    public int updateCheckStatus(MStoreOperateLog request) {
        try {
            log.info("修改记录");
            MStoreOperateLog param = new MStoreOperateLog();
            param.setUpdater(request.getAdminOperate().getOperator());
            param.setUpdateTime(new Date());
            BeanUtils.copyProperties(request, param);
            int flag = mapper.updateCheckStatus(param);
            if (flag == 1) {
                return 1;
            } else {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
            }
        } catch (Exception e) {
            log.error("修改记录失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
        }

    }

    /**
     * 记录详情
     *
     * @param request 查询的入参实体类
     * @return 数据库查询到的实体类
     */
    @Override
    public MStoreOperateLog getByPrimaryKey(MStoreOperateLog request) {
        return mapper.selectByPrimaryKey(request);
    }


    /**
     * 分页查询
     *
     * @param request 分页查询入参实体类
     * @return 含有分页信息的实体类列表
     */
    @Override
    public PageResult<MStoreOperateResponse> listPageBySelective(MStoreOperateRequest request, int pageNum, int pageSize) {
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<MStoreOperateLog> mStores = mapper.listPageBySelective(request);
        PageInfo<MStoreOperateLog> pageInfo = new PageInfo<MStoreOperateLog>(mStores);
        List<MStoreOperateResponse> collect = pageInfo.getList().stream().map(temp -> {
            MStoreOperateResponse bo = new MStoreOperateResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MStoreOperateResponse> result = new PageResult<>();
        result.setTotal(pageInfo.getTotal());
        result.setRows(collect);
        return result;
    }


    @Override
    public PageResult<MStoreOperateResponse> listOpratePageBySelective(MStoreOperateRequest request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if (request.getCreateTimeStartAndEnd()!=null && request.getCreateTimeStartAndEnd().size()>0){
            request.setCreateTimeStart(request.getCreateTimeStartAndEnd().get(0));
            request.setCreateTimeEnd(request.getCreateTimeStartAndEnd().get(1));
        }
        List<MStoreOperateLog> mStores = mapper.listOpratePageBySelective(request);
        PageInfo<MStoreOperateLog> pageInfo = new PageInfo<MStoreOperateLog>(mStores);
        List<MStoreOperateResponse> collect = pageInfo.getList().stream().map(temp -> {
            MStoreOperateResponse bo = new MStoreOperateResponse();
            BeanUtils.copyProperties(temp, bo);
            MStore mStore = new MStore();
            mStore.setDeleteFlag(0);
            mStore.setId(temp.getStoreId());
            MStore store = mStoreMapper.selectByPrimaryKey(mStore);
            if (null != store) {
                bo.setStoreName(store.getName());
            }

//            RestResult<String> dept = sysDeptApi.queryDeptNameById(bo.getDeptId());
//            if(null!=dept){
//                bo.setDeptName(dept.getData());
//            }
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MStoreOperateResponse> result = new PageResult<>();
        result.setTotal(pageInfo.getTotal());
        result.setRows(collect);
        return result;
    }

    @Override
    public PageResult<MStoreCheckResponse> listOperateAndProductPage(MStoreOperateRequest request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MStoreCheckResponse> mStores = mapper.listOperateAndProductPage(request);
        PageInfo<MStoreCheckResponse> pageInfo = new PageInfo<>(mStores);
        List<MStoreCheckResponse> collect = pageInfo.getList().stream().map(temp -> {
            MStoreCheckResponse bo = new MStoreCheckResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MStoreCheckResponse> result = new PageResult<>();
        result.setTotal(pageInfo.getTotal());
        result.setRows(collect);
        return result;
    }

    /**
     * 不分页查询
     *
     * @param request 不分页查询入参实体类
     * @return 数据库查到的实体类列表
     */
    @Override
    public List<MStoreOperateLog> listAllRecord(MStoreOperateLog request) {
        //分页处理
        request.setDeleteFlag(0);
        return mapper.selectBySelective(request);
    }

    /**
     * 导入库内事物都详情
     *
     * @param fileName
     * @param file
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult mStoreOperateDetailExcel(String fileName, MultipartFile file, Long id) throws IOException {
        //验证EXCEL文件
        if (!ExcelImportUtils.validateExcel(fileName)) {
            return RestResult.error("9999", "上传文件格式不正确");
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        Map<String, PictureData> mapList = null;
        //店铺商品
        Sheet sheet = null;
        //2003的excel
        if (ExcelImportUtils.isExcel2003(fileName)) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        sheet = wb.getSheetAt(0);
        if (sheet == null) {
            return RestResult.error("9999", "数据不存在");
        }
        //r = 2 表示从第三行开始循环 如果你的第三行开始是数据
        List<MStoreOperateProductInfoResponse> mStoreOperateProductInfoResponseList = new ArrayList<>();
        for (int r = 1; r <= sheet.getLastRowNum(); r++) {
            //通过sheet表单对象得到 行对象
            Row row = sheet.getRow(r);
            if (row == null) {
                continue;
            }
            //导入数据请求
            Product product = new Product();
            MStoreOperateProductInfoResponse mStoreOperateProductInfoResponse = new MStoreOperateProductInfoResponse();
            //共4列表数据
            for (int j = 0; j < 4; j++) {
                Cell cell = row.getCell(j);
                if (cell != null) {
                    cell.setCellType(CellType.STRING);
                    if (j == 0) {
                        if (StringUtils.isEmpty(cell.getStringCellValue())) {
                            return RestResult.error("9999", "第" + r + "行的商品编码为空，请检查");
                        }
                        product.setCode(cell.getStringCellValue());
                    } else if (j == 1) {
                        if (StringUtils.isEmpty(cell.getStringCellValue())) {
                            return RestResult.error("9999", "第" + r + "行的商品名称为空，请检查");
                        }
                        product.setChineseName(cell.getStringCellValue());
                    } else if (j == 2) {
                        if (StringUtils.isEmpty(cell.getStringCellValue())) {
                            return RestResult.error("9999", "第" + r + "行的申请为空，请检查");
                        }
                        mStoreOperateProductInfoResponse.setApplyNum(Long.valueOf(cell.getStringCellValue()));
                    } else {
                        if (StringUtils.isEmpty(cell.getStringCellValue())) {
                            return RestResult.error("9999", "第" + r + "行的申请为空，请检查");
                        }
                        mStoreOperateProductInfoResponse.setActualNum(Long.valueOf(cell.getStringCellValue()));
                    }
                }
            }
            List<Product> productList = productMapper.queryAll(product);
            if (CollectionUtils.isEmpty(productList)) {
                return RestResult.error("9999", "第" + r + "行的商品不存在,请检查");
            } else {
                mStoreOperateProductInfoResponse.setProductId(productList.get(0).getId());
                mStoreOperateProductInfoResponse.setCode(product.getCode());
                mStoreOperateProductInfoResponse.setChineseName(product.getChineseName());
                mStoreOperateProductInfoResponse.setArtNo(productList.get(0).getArtNo());
                mStoreOperateProductInfoResponse.setBarCode(productList.get(0).getBarCode());
                //商品计量单位
                ProductUnits units = productUnitsMapper.queryByPrimaryKey(productList.get(0).getSaleCalcUnitId());
                if (null != units) {
                    mStoreOperateProductInfoResponse.setUnitName(units.getName());
                }
                //可用库存
                StoreInventory storeInventory = new StoreInventory();
                storeInventory.setProductId(product.getId());
                storeInventory.setDeleteFlag(0);
                List<StoreInventory> inventoryList = storeInventoryService.query(storeInventory);
                if (CollectionUtils.isNotEmpty(inventoryList)) {
                    mStoreOperateProductInfoResponse.setAvailableStock(inventoryList.get(0).getAvailableStock());
                }
                mStoreOperateProductInfoResponseList.add(mStoreOperateProductInfoResponse);
            }
        }
        return RestResult.success(mStoreOperateProductInfoResponseList);
    }


    @Override
    public List<MStoreOperateLog> listExcel(MStoreOperateRequest request) {
        return mapper.listExcel(request);
    }

    /**
     * 实体库存变动流水列表分页查询
     *
     * @param request 分页查询入参实体类
     * @return 含有分页信息的实体类列表
     */
    @Override
    public PageResult<MStoreOperateResponse> operateListPage(MStoreOperateRequest request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if(request.getCreateTimeStartAndEnd()!=null && request.getCreateTimeStartAndEnd().size()>1){
            request.setCreateTimeStart(request.getCreateTimeStartAndEnd().get(0));
            request.setCreateTimeEnd(request.getCreateTimeStartAndEnd().get(1));
        }
        List<MStoreOperateLog> mStores = mapper.operateListPage(request);
        PageInfo<MStoreOperateLog> pageInfo = new PageInfo<MStoreOperateLog>(mStores);
        List<MStoreOperateResponse> collect = pageInfo.getList().stream().map(temp -> {
            MStoreOperateResponse bo = new MStoreOperateResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MStoreOperateResponse> result = new PageResult<>();
        result.setTotal(pageInfo.getTotal());
        result.setRows(collect);
        return result;
    }


    /**
     * 实体库存变动流水列表分页查询
     *
     * @param request 分页查询入参实体类
     * @return 含有分页信息的实体类列表
     */
    @Override
    public List<MStoreOperateLog> listMStoreOperateExcel(MStoreOperateRequest request) {
        List<MStoreOperateLog> mStores = mapper.operateListPage(request);
        return mStores;
    }

}
